package cn.wolfcode.business.service.impl;

import java.util.List;

import cn.wolfcode.business.domain.BusAppointment;
import cn.wolfcode.business.mapper.BusAppointmentMapper;
import cn.wolfcode.common.constant.BusinessConstants;
import cn.wolfcode.common.utils.DateUtils;
import cn.wolfcode.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.wolfcode.business.mapper.BusStatementMapper;
import cn.wolfcode.business.domain.BusStatement;
import cn.wolfcode.business.service.IBusStatementService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * 结算单Service业务层处理
 * 
 * @author zhangyf
 * @date 2023-04-13
 */
@Service
public class BusStatementServiceImpl implements IBusStatementService 
{
    @Autowired
    private BusStatementMapper busStatementMapper;
    @Autowired
    private BusAppointmentMapper busAppointmentMapper;

    /**
     * 查询结算单
     * 
     * @param id 结算单主键
     * @return 结算单
     */
    @Override
    public BusStatement selectBusStatementById(Long id)
    {
        return busStatementMapper.selectBusStatementById(id);
    }

    /**
     * 查询结算单列表
     * 
     * @param busStatement 结算单
     * @return 结算单
     */
    @Override
    public List<BusStatement> selectBusStatementList(BusStatement busStatement)
    {

        busStatement.setIsDelete(BusinessConstants.STATEMENT_STATUS_NOT_DEL);
        return busStatementMapper.selectBusStatementList(busStatement);
    }

    /**
     * 新增结算单
     * 
     * @param busStatement 结算单
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBusStatement(BusStatement busStatement)
    {
        //todo 参数校验

        // 只有一个预约单 id
        if(busStatement.getAppointmentId() != null){
            // 通过预约生成结算单
            // 获取对应预约单（根据预约单id查询预约单）
            BusAppointment busAppointment = busAppointmentMapper.selectBusAppointmentById(busStatement.getAppointmentId());

            // 将预约单状态改成已结算
            busAppointment.setStatus(BusinessConstants.APPOINTMENT_STATUS_SETTLED);
            busAppointmentMapper.updateBusAppointment(busAppointment);

            busStatement.setCustomerName(busAppointment.getCustomerName());
            busStatement.setCustomerPhone(busAppointment.getCustomerPhone());
            busStatement.setServiceType(busAppointment.getServiceType());
            busStatement.setCarSeries(busAppointment.getCarSeries());
            busStatement.setLicensePlate(busAppointment.getLicensePlate());
            busStatement.setInfo(busAppointment.getInfo());
            busStatement.setActualArrivalTime(busAppointment.getActualArrivalTime());


        }
            // 不是通过预约生成结算单
            busStatement.setIsDelete(BusinessConstants.STATEMENT_STATUS_NOT_DEL);
            busStatement.setStatus(BusinessConstants.STATEMENT_STATUS_CONSUMING);
            busStatement.setCreateBy(SecurityUtils.getUsername());
            busStatement.setCreateTime(DateUtils.getNowDate());

        return busStatementMapper.insertBusStatement(busStatement);
    }

    /**
     * 修改结算单
     * 
     * @param busStatement 结算单
     * @return 结果
     */
    @Override
    public int updateBusStatement(BusStatement busStatement)
    {
        // 参数校验
        // 状态判断
        BusStatement originBusStatement = busStatementMapper.selectBusStatementById(busStatement.getId());
        Assert.notNull(originBusStatement,"非法参数");
        int status = originBusStatement.getStatus();
        Assert.state(status == BusinessConstants.STATEMENT_STATUS_CONSUMING,"不是消费中不可以修改的");
        int isDelete = originBusStatement.getIsDelete();
        Assert.state(isDelete == BusinessConstants.STATEMENT_STATUS_NOT_DEL,"删除的不可以修改的");

        return busStatementMapper.updateBusStatement(busStatement);
    }

    /**
     * 批量删除结算单
     * 
     * @param ids 需要删除的结算单主键
     * @return 结果
     */
    @Override
    public int deleteBusStatementByIds(Long[] ids)
    {
        return busStatementMapper.deleteBusStatementByIds(ids);
    }

    /**
     * 删除结算单信息
     * 
     * @param id 结算单主键
     * @return 结果
     */
    @Override
    public int deleteBusStatementById(Long id)
    {
        return busStatementMapper.deleteBusStatementById(id);
    }

    @Override
    public BusStatement selectByAppointmentId(Long appointmentId) {
        BusStatement busStatement =  busStatementMapper.selectByAppointmentId(appointmentId);
        return busStatement;
    }

    @Override
    public int updateDeletedStatusById(Long id) {
        BusStatement originBusStatement = busStatementMapper.selectBusStatementById(id);
        Assert.notNull(originBusStatement,"非法参数");
        Assert.state(originBusStatement.getIsDelete() == BusinessConstants.STATEMENT_STATUS_NOT_DEL,"删除的不能被删除");
        originBusStatement.setIsDelete(BusinessConstants.STATEMENT_STATUS_DEL);

        return busStatementMapper.updateBusStatement(originBusStatement);
    }
}
